const app = getApp();
import {
  decrypt,
  getOneId,
  assignDetail,
  takeReceive,
  giftsClue
} from "../../../server/api";
Page({
  data: {
    oss_url: app.globalData.oss_url,
    authorShow:false,
    buttons:[{text: '取消'}, {text: '去授权'}],
    active:1,
    phone:'',
    haveORno:0,   //是否有礼金券
    from:'qrCode',  //用来判断扫码进入还是tabbar进入
    fromTakeBtn:false,   //按钮控制
    giftId:4,
    giftMsg:{},
    custName:'',   //微信昵称
    deptsInfo:{}    //经销商信息
  },
  onLoad(options){
    console.log(options)  //扫码进来拿到的带参至：少带 from,礼金券ID
    this.getAssignDetail()
    if(this.data.from=='qrCode'){
      this.setData({
        fromTakeBtn:true,
        giftId:options.giftsId
      })
    }
  
  },
  onShow: function () {
    if(typeof this.getTabBar=== 'function' && this.getTabBar()){
      this.getTabBar().setData({
        active:1
      })
    }
    this.setData({
      haveORno:app.globalData.holdGifts,
      custName:app.globalData.user_info.nickName,
      deptsInfo:app.globalData.deptsInfo
    })

  },
  //领取礼金券
  takeReceive(){
    let data={
      oneId:app.globalData.user_info.oneId,
     // giftsId:this.data.giftId,
      giftsId:4,
      deptId:this.data.deptsInfo.deptsId,
     // deptId:123,
      custName:this.data.custName,
     // custName:'nikeName',
      custPhone:Number(this.data.phone)
    }
    console.log(data)
    takeReceive(data).then(res=>{
      if(res.code==0){
        wx.showToast({
          title: '领取成功',
          icon:'none'
        })
        wx.switchTab({
          url: '/pages/gift/myGiftBag/index?giftId='+this.data.giftId,
        })
       this.goGiftsClue()
      }else{
        wx.showToast({
          title: res.errMsg,
          icon:'none'
        })
      }
    })
  },
  //生成线索
  goGiftsClue(){
    let data={
      //  app.globalData.user_info.oneId,
      oneId: 350,
      customerName: this.data.custName,
    // customerName:'nickName',
      mobile: this.data.phone,
      city: app.globalData.city,
      dealerIds:123, // this.data.deptsInfo.deptsId,  //经销商id
      client: 'program',
      firstChannelName:'客户端',
      secondChannelName:'小程序',
    }
    giftsClue(data).then(res=>{
      if(res.code==0){
        console.log('领取成功，生成一条线索！')
      }else{
        wx.showToast({
          title: res.errMsg,
          icon:'none'
        })
      }
    })
  },
  getAssignDetail(){
    let id={
      //  app.globalData.user_info.userDeptId  所属部门id
      deptId:124  
    }
    assignDetail(id).then(res=>{
      if(res.code==0){
        this.setData({
          giftMsg:res.data,
          giftId:res.data.id
        })
      }else{
        wx.showToast({
          title: res.errMsg,
          icon:'none'
        })
      }
    })
  },
  get_phone(e) {
    console.log(e)
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let obj = {}
      obj['openId'] = app.globalData.user_info.openId
      obj['session_key'] = app.globalData.session_key
      obj['encryptedData'] = e.detail.encryptedData
      obj['iv'] = e.detail.iv
      decrypt(obj).then(res => {
        if (res.code == 0) {
          app.globalData.user_info.phone =Number(res.data.phone) 
          this.setData({ phone: app.globalData.user_info.phone })
          let obj_phone = {}
          obj_phone['openId'] = app.globalData.user_info.openId
          obj_phone['phone'] = app.globalData.user_info.phone;
          this.takeReceive()
        }
      })
    } else {
      wx.showToast({
        title: '未进行微信授权无法领取礼金包',
        icon: 'none',
        duration: 2000
      })
    }
  },
  getOneId() {
    let data = {
      openId: app.globalData.user_info.openId,
      phone: this.data.phone
    }
    getOneId(data).then(res => {
      if (res.code == 0) {
        this.setData({
          oneId: res.data
        })
      }
    })
  },
})